package com.booking.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.booking.bean.Food;
import com.booking.bean.Seller;
import com.booking.bean.Users;
import com.booking.dao.FoodDAO;
import com.booking.dao.SellerDAO;

@Repository
public class FoodDAOImpl implements FoodDAO {

	@Autowired
	private SessionFactory sessionFactory;
	//获取和当前线程绑定的 Session. 
	public Session getSession(){
		return sessionFactory.getCurrentSession();
	}
	
	@Transactional
	@Override
	public int findCount() {
		String hql="select count(*) from Food";
		
		org.hibernate.Query query = this.getSession().createQuery(hql); 
		
		List list = query.list();
		if(list.size()>0)
			return  Integer.parseInt(String.valueOf(list.get(0))) ;
		
		return 0;
	}
	
	@Transactional
	@Override
	public List<Food> findByPage(int offset, int length,int sId) {
	        List<Food> entitylist=null;
	        try{
	            Query query = this.getSession().createQuery("from Food Where sellerID = ?");
	            query.setFirstResult(offset);
	            query.setMaxResults(length);
	    		query.setInteger(0,sId);
	            entitylist = query.list();
	            
	        }catch(RuntimeException re){
	            throw re;
	        }
	        
	        return entitylist;
	    }

	@Transactional
	@Override
	public Food findById(Integer foodId) {
		String hql=" from Food where ID = ?";
		
		Query query= this.getSession().createQuery(hql);
		
		query.setLong(0,foodId);
				
		List<Food> list = query.list();
		
	//	this.getSession().close();
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	@Transactional
	@Override
	public String saveFood(Food food) {
		String result = null;
		
		result =  String.valueOf(this.getSession().save(food)) ;
		
		return result;

	}
	
	@Transactional
	@Override
	public List<Food> queryFood(Integer sellerId) {
	String hql=" from Food where sellerID = ?";
		
		Query query= this.getSession().createQuery(hql);
		
		query.setLong(0,sellerId);
				
		List<Food> list = query.list();
		
	//	this.getSession().close();
		if(list.size()>0)
			return list;
		return null;
	}
	@Transactional
	@Override
	public void deleteByIds(String ids) {
		String hql = "delete from Food f where f.id in (" + ids + ")";
		this.getSession().createQuery(hql).executeUpdate();
	}

	@Transactional
	@Override
	public String updateFood(Food food) {
		if(food.getPic() != null){
		String sql="update Food f set f.foodname='"+food.getFoodname()+"', f.price="+food.getPrice()+",f.mark='"+food.getMark()+"',"
				+ "f.pic='"+food.getPic()+"' where f.id = "+food.getId()+"";
/*		String sql="update Food f set f.foodname="+food.getFoodname()+" where f.id = "+food.getId()+"";
*/		
		this.getSession().createQuery(sql).executeUpdate();
		}
		if(food.getPic() == null){
			String sql="update Food f set f.foodname='"+food.getFoodname()+"', f.price="+food.getPrice()+",f.mark='"+food.getMark()+"'"
					+ " where f.id = "+food.getId()+"";
			/*		String sql="update Food f set f.foodname="+food.getFoodname()+" where f.id = "+food.getId()+"";
			 */		
			this.getSession().createQuery(sql).executeUpdate();
		}
		return null;
	}

}
